<?php

/**
 * Hoa Framework
 *
 *
 * @license
 *
 * GNU General Public License
 *
 * This file is part of HOA Open Accessibility.
 * Copyright (c) 2007, 2008 Ivan ENDERLIN. All rights reserved.
 *
 * HOA Open Accessibility is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * HOA Open Accessibility is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HOA Open Accessibility; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 * @category    Framework
 * @package     Hoa_Convertor
 *
 */

/**
 * Hoa_Framework
 */
require_once 'Framework.php';

/**
 * Hoathis_Convertor_Format_Abstract
 */
importModule('Convertor.Format.Abstract');

/**
 * Hoa_Stream_Context
 */
import('Stream.Context');

/**
 * Hoa_File
 */
import('File.~');

/**
 * Class Hoathis_Convertor_Format_Translation
 *
 * Extending the Hoathis_Convertor_Format_Abstract class.
 *
 * @author      Gregory DARCHE <tetardo@gmail.com>
 * @copyright   Copyright (c) 2009 Gregory DARCHE.
 * @license     http://gnu.org/licenses/gpl.txt GNU GPL
 * @since       PHP 5
 * @version     0.1
 * @package     Hoathis_Convertor
 * @subpackage  Hoathis_Convertor_Format
 */
Class Hoathis_Convertor_Format_Translation extends Hoathis_Convertor_Format_Abstract {

    protected $units = array(
        'af' => 'AFRIKAANS',
        'sq' => 'ALBANIAN',
        'am' => 'AMHARIC',
        'ar' => 'ARABIC',
        'hy' => 'ARMENIAN',
        'az' => 'AZERBAIJANI',
        'eu' => 'BASQUE',
        'be' => 'BELARUSIAN',
        'bn' => 'BENGALI',
        'bh' => 'BIHARI',
        'bg' => 'BULGARIAN',
        'my' => 'BURMESE',
        'ca' => 'CATALAN',
        'chr' => 'CHEROKEE',
        'zh' => 'CHINESE',
        'zh-CN' => 'CHINESE_SIMPLIFIED',
        'zh-TW' => 'CHINESE_TRADITIONAL',
        'hr' => 'CROATIAN',
        'cs' => 'CZECH',
        'da' => 'DANISH',
        'dv' => 'DHIVEHI',
        'nl' => 'DUTCH',
        'en' => 'ENGLISH',
        'eo' => 'ESPERANTO',
        'et' => 'ESTONIAN',
        'tl' => 'FILIPINO',
        'fi' => 'FINNISH',
        'fr' => 'FRENCH',
        'gl' => 'GALICIAN',
        'ka' => 'GEORGIAN',
        'de' => 'GERMAN',
        'el' => 'GREEK',
        'gn' => 'GUARANI',
        'gu' => 'GUJARATI',
        'iw' => 'HEBREW',
        'hi' => 'HINDI',
        'hu' => 'HUNGARIAN',
        'is' => 'ICELANDIC',
        'id' => 'INDONESIAN',
        'iu' => 'INUKTITUT',
        'ga' => 'IRISH',
        'it' => 'ITALIAN',
        'ja' => 'JAPANESE',
        'kn' => 'KANNADA',
        'kk' => 'KAZAKH',
        'km' => 'KHMER',
        'ko' => 'KOREAN',
        'ku' => 'KURDISH',
        'ky' => 'KYRGYZ',
        'lo' => 'LAOTHIAN',
        'lv' => 'LATVIAN',
        'lt' => 'LITHUANIAN',
        'mk' => 'MACEDONIAN',
        'ms' => 'MALAY',
        'ml' => 'MALAYALAM',
        'mt' => 'MALTESE',
        'mr' => 'MARATHI',
        'mn' => 'MONGOLIAN',
        'ne' => 'NEPALI',
        'no' => 'NORWEGIAN',
        'or' => 'ORIYA',
        'ps' => 'PASHTO',
        'fa' => 'PERSIAN',
        'pl' => 'POLISH',
        'pt-PT' => 'PORTUGUESE',
        'pa' => 'PUNJABI',
        'ro' => 'ROMANIAN',
        'ru' => 'RUSSIAN',
        'sa' => 'SANSKRIT',
        'sr' => 'SERBIAN',
        'sd' => 'SINDHI',
        'si' => 'SINHALESE',
        'sk' => 'SLOVAK',
        'sl' => 'SLOVENIAN',
        'es' => 'SPANISH',
        'sw' => 'SWAHILI',
        'sv' => 'SWEDISH',
        'tg' => 'TAJIK',
        'ta' => 'TAMIL',
        'tl' => 'TAGALOG',
        'te' => 'TELUGU',
        'th' => 'THAI',
        'bo' => 'TIBETAN',
        'tr' => 'TURKISH',
        'uk' => 'UKRAINIAN',
        'ur' => 'URDU',
        'uz' => 'UZBEK',
        'ug' => 'UIGHUR',
        'vi' => 'VIETNAMESE',
        'cy' => 'WELSH',
        'yi' => 'YIDDISH',
        ''   => 'UNKNOWN'
    );

    protected $filters = array(
        'Censure'
    );

    protected function convert( $value, $from, $to ) {

        Hoa_Stream_Context::getInstance('google', 'http')->addOptions(array(
            'timeout' => 30
        ));

        try {

            $content = new Hoa_File(
                'http://translate.google.com/translate_t?text='.urlencode($value).'&langpair='.$from.'&hl='.$to.'&ie=UTF-8&oe=UTF-8',
                Hoa_File::MODE_READ,
                'google'
            );
        }
        catch ( Hoa_File_Exception $e ) {

            throw new Hoa_Convertor_Exception(
                $e->getFormattedMessage(),
                $e->getCode()
            );
        }

        $content = $content->readAll();

        $content = strstr($content, '<div id=result_box dir="ltr">');
        $end_content = strstr($content, '</div>');

        $proposition = str_replace($end_content, '', $content);
        $proposition = str_replace('<div id=result_box dir="ltr">', '', $proposition);

        return $proposition;
    }
}